package relatorios;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;

public class ReportUtils {

	public static void exportMultipleReportsToPdfStream(List<JasperPrint> prints, OutputStream output) throws JRException {
		if (prints.isEmpty())
			return;
		JRPdfExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, prints);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
		exporter.exportReport();
	}
	
	public static void exportReportToDocxStream(JasperPrint print, String outFileName) throws JRException {
		JRDocxExporter exporter = new JRDocxExporter();
		exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new File(outFileName));
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
		exporter.exportReport();
	}

	public static void exportByteArrayToPdfStream(byte[] bytes, HttpServletResponse response) throws JRException, IOException {
		if (bytes != null && bytes.length > 0) {
			response.setContentType("application/pdf");
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
		}
	}

}
